<?php

namespace App\Repositories;

use App\Enum\DriverEnum;
use App\Models\DriverWithdraw;
use BobCoder\Construct\Foundation\Repository\Repository;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

class WithdrawRepository extends Repository
{
    protected $model;

    public function __construct()
    {
        $this->model = new DriverWithdraw();
    }


    /**
     * 提现列表
     * @param int $companyId 公司ID
     * @param array $date 提现时间
     * @param string $status 提现状态
     * @param int $pageSize
     * @return mixed
     * @Author 姿势就是力量
     */
    public function getPageList(int $companyId,string $date, string $status,string $withdraw_sn,string $money,string $real_name,string $phone,string $remit_sn,string $account,string $bank_name,string $account_name,string  $review_time, string $type,string $rules,int $pageSize = 10)
    {
        return $this->model
             ->where('type',$rules)
            ->company($companyId)
            ->with([
                'driver:id,real_name,phone',
                'admin:id,name',
                'wallet:id,type,account,bank_name,account_name'
            ])
            ->when($status, function ($query) use ($status) {
                $query->where('status', $status);          //状态
            })
            ->when($date,function ($query)use($date){
                if($date){
                    $list = explode(',',$date);
                    $query->whereBetween('created_at',[$list[0],$list[1]]); //时间选择
                }
            })
            ->when($review_time, function ($query) use ($review_time) {
                if($review_time){
                    $list = explode(',',$review_time);
                    $query->whereBetween('review_time', [$list[0],$list[1]]);  //审核时间
                }
            })
            ->when($withdraw_sn,function($query) use ($withdraw_sn){
                $query->where('withdraw_sn','like','%'.$withdraw_sn.'%');  //提现流水号
            })
            ->when($money,function($query) use ($money){
                $query->where('money','like','%'.$money.'%');  //提现金额
            })
            ->when($remit_sn,function($query) use ($remit_sn){
                $query->where('remit_sn','like','%'.$remit_sn.'%');  //第三方流水号
            })
            ->when($real_name,function ($query) use ($real_name){
                $query->whereHas('driver',function ($query2) use ($real_name){
                    $query2->where('real_name','like','%'.$real_name.'%');    //司机姓名
                });
            })
            ->when($phone,function ($query) use ($phone){
                $query->whereHas('driver',function ($query2) use($phone){
                   $query2->where('phone','like','%'.$phone.'%');             //司机手机号
                });
            })
            ->when($account,function ($query) use ($account){
                $query->whereHas('wallet',function ($query2) use($account){
                    $query2->where('account','like','%'. $account.'%');             //提现账号
                });
            })
            ->when($bank_name,function ($query) use ($bank_name){
                $query->whereHas('wallet',function ($query2) use($bank_name){
                    $query2->where('bank_name','like','%'.$bank_name.'%');             //银行名称
                });
            })
            ->when($account_name,function ($query) use ($account_name){
                $query->whereHas('wallet',function ($query2) use($account_name){
                    $query2->where('account_name','like','%'.$account_name.'%');             //开户姓名
                });
            })
            ->when($type,function ($query) use ($type){
                 $query->whereHas('wallet',function ($query2) use($type){
                        $query2->where('type','like','%'.$type.'%');             //提现渠道
                    });
            })
            ->orderBy('created_at', 'desc')
            ->paginate($pageSize)
            ->toArray();
    }


    public function Excel_getPageList(int $companyId,$companyIds,string $date, string $status,string $withdraw_sn,string $money,string $real_name,string $phone,string $remit_sn,string $account,string $bank_name,string $account_name,string  $review_time, string $type)
    {

        return $this->model
            ->with([
                'driver:id,real_name,phone',
                'admin:id,name',
                'wallet:id,type,account,bank_name,account_name'
            ])
            ->when($status, function ($query) use ($status) {
                $query->where('status', $status);          //状态
            })
            ->when($companyId, function ($query) use ($companyId) {
                $query->where('company_id', $companyId);                   //查询公司
            })
            ->when($date,function ($query)use($date){
                if($date){
                    $list = explode(',',$date);
                    $query->whereBetween('created_at',[$list[0],$list[1]]); //时间选择
                }
            })
            ->when($review_time, function ($query) use ($review_time) {
                if($review_time){
                    $list = explode(',',$review_time);
                    $query->whereBetween('review_time', [$list[0],$list[1]]);  //审核时间
                }
            })
            ->when($withdraw_sn,function($query) use ($withdraw_sn){
                $query->where('withdraw_sn','like','%'.$withdraw_sn.'%');  //提现流水号
            })
            ->when($money,function($query) use ($money){
                $query->where('money','like','%'.$money.'%');  //提现金额
            })
            ->when($remit_sn,function($query) use ($remit_sn){
                $query->where('remit_sn','like','%'.$remit_sn.'%');  //第三方流水号
            })
            ->when($real_name,function ($query) use ($real_name){
                $query->whereHas('driver',function ($query2) use ($real_name){
                    $query2->where('real_name','like','%'.$real_name.'%');    //司机姓名
                });
            })
            ->when($phone,function ($query) use ($phone){
                $query->whereHas('driver',function ($query2) use($phone){
                    $query2->where('phone','like','%'.$phone.'%');             //司机手机号
                });
            })
            ->when($account,function ($query) use ($account){
                $query->whereHas('wallet',function ($query2) use($account){
                    $query2->where('account','like','%'. $account.'%');             //提现账号
                });
            })
            ->when($bank_name,function ($query) use ($bank_name){
                $query->whereHas('wallet',function ($query2) use($bank_name){
                    $query2->where('bank_name','like','%'.$bank_name.'%');             //银行名称
                });
            })
            ->when($account_name,function ($query) use ($account_name){
                $query->whereHas('wallet',function ($query2) use($account_name){
                    $query2->where('account_name','like','%'.$account_name.'%');             //开户姓名
                });
            })
            ->when($type,function ($query) use ($type){
                $query->whereHas('wallet',function ($query2) use($type){
                    $query2->where('type','like','%'.$type.'%');             //提现渠道
                });
            })
            ->orderBy('created_at', 'desc')
            ->whereIn('company_id',$companyIds)
            ->get()
            ->toArray();
    }

    /**
     * 统计提现金额
     * @param int $companyId 公司ID
     * @param array $date 日期
     * @param string $status 状态
     * @return mixed
     * @Author 姿势就是力量
     */
    public function sumMoney(int $companyId, string $date, string $status)
    {
        return $this->model->company($companyId)
            ->when($date,function ($query)use($date){
                if($date){
                    $list = explode(',',$date);
                    $query->whereBetween('created_at',[$list[0],$list[1]]); //时间选择
                }
            })
            ->when($status, function ($query) use ($status) {
                $query->where('status', $status);              //转态
            })
            ->sum('money');
    }


    /**
     * 保存数据
     * @param array $params
     * @return DriverWithdraw|Collection|Model
     * @Author 姿势就是力量
     */
    public function saveData(array $params)
    {
        if (!empty($params['id'])) {
            $model = $this->find($params['id']);
        } else {
            $model = $this->model;
        }

        foreach ($params as $key => $value) {
            $model->{$key} = $value;
        }
        $model->admin_id = session('adminInfo.id');
        $model->review_time = Carbon::now()->toDateTimeString();
        $model->save();

        return $model;
    }


}
